
import xlrd,xlwt
from django.http import HttpResponse


class ExcelData():
    def __init__(self, file_path, sheet_name):
        self.file_path = file_path
        self.sheet_name = sheet_name
        self.file_data = xlrd.open_workbook(self.file_path)
        self.table_data = self.file_data.sheet_by_index(0)
        self.keys = self.table_data.row_values(0)
        self.row_num = self.table_data.nrows
        self.col_num = self.table_data.ncols

    def writeExcel(self):

        response=HttpResponse(content_type='application/vnd.ms-excel')
        response['Content-Disposition'] = 'attachment;filename=order.xls'
        head_data = [u'订单号', u'下单时间', u'产品名称', u'数量', u'订单金额', u'支付金额', u'支付方式',
                     u'客户姓名', u'客户手机', u'收货人省份', u'收货人地市', u'收货人县区', u'收货人地址', u'备注']
        # 2.创建excel
        wx = xlwt.Workbook(encoding='uft-8')
        sheet = wx.add_sheet('Sheet1')

        #写入标题
        for col in range(len(head_data)):
            sheet.write(0, col, head_data[col])

        #写入数据
        # for



    def readExcel(self):
        sheet_data=[]
        for row in range(1, self.row_num):
            row_data={}
            for col in range(self.col_num):
                # c_type = self.table_data.cell(row, col)
                # if c_type
                c_data = self.table_data.cell_value(row, col)
                row_data[self.keys[col]] = c_data
            sheet_data.append(row_data)

        return sheet_data

if __name__ == "__main__":
    # get_data = ExcelData("test.xls","sheet1")
    # datas = get_data.readExcel()
    # print(datas)
    # print("000")
    head_data = [u'订单号', u'下单时间', u'产品名称', u'数量', u'订单金额', u'支付金额', u'支付方式',
                 u'客户姓名', u'客户手机', u'收货人省份', u'收货人地市', u'收货人县区', u'收货人地址', u'备注']
    print(len(head_data))
    print(head_data[0])